﻿using System.Windows;
using WPFSchematics.Definition.Entities;

namespace WPFSchematics.Graphics.Ghosts
{
    /// <summary>
    /// 幻影宿主类。
    /// </summary>
    public abstract class GhostHost : Entity,IGhostHost
    {
        /// <summary>
        /// 幻影观察器。
        /// </summary>
        public GhostObserver Observer { get; private set; }

        /// <summary>
        /// 实时幻影。
        /// </summary>
        public GhostObject Ghost
        {
            get { return Observer.SnapGhost(true); }
        }

        /// <summary>
        /// 属性变更处理。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (Observer != null)
            {
                Observer.OnPropertyChanged(e);
            }
            base.OnPropertyChanged(e);
        }

        /// <summary>
        /// 注册幻影属性。该属性可进行幻影截取。
        /// </summary>
        public virtual void RegisterGhostProperty()
        {

        }

        /// <summary>
        /// 添加子宿主对象。
        /// </summary>
        /// <param name="collectionProperty">宿主对象中包含子宿主对象的集合。</param>
        /// <param name="observerHost">子宿主对象。</param>
        /// <param name="index"></param>
        protected void AddChildObserverHost(DependencyProperty collectionProperty, IGhostHost observerHost,int index)
        {
            Observer.AddChildGhostObserver(collectionProperty, observerHost.Observer,index);
        }

        /// <summary>
        /// 移除子宿主对象。
        /// </summary>
        /// <param name="collectionProperty">宿主对象中包含子宿主对象的集合。</param>
        /// <param name="observerHost">子宿主对象。</param>
        /// <param name="index"></param>
        protected void RemoveChildObserverHost( DependencyProperty collectionProperty, IGhostHost observerHost,int index)
        {
            Observer.RemoveChildGhostObserver(collectionProperty, observerHost.Observer,index);
        }

        /// <summary>
        /// 创建新实例。
        /// </summary>
        /// <returns></returns>
        public virtual DependencyObject CreateInstance()
        {
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        private void Initialize()
        {
            Observer = new GhostObserver(this);
            RegisterGhostProperty();
        }

        /// <summary>
        /// 构造。
        /// </summary>
        protected GhostHost()
        {
            Initialize();
        }
    }
}
